using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.Interfaces;
using FiltronASP.Models.DAO.WIX;
using FiltronASP.Models.DAO.Interfaces.WIX;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Models.DAO.Interfaces.Filtron;
using System.Text.RegularExpressions;
using FiltronASP.Helpers.Translations;

namespace FiltronASP.Helpers.ReplacementsHelper
{
    public class ReplacementList
    {
        public static string generateReplacementList(string typedValue, string languageName) {
            typedValue = removeSpecialCharacters(typedValue);
            if (typedValue.Length < 2) {
                return String.Empty;
            }                        
            IWixInterchangeDAO wixInterchangeDAO = new WixInterchangeDAO();
            IFiltronInterchangeDAO filtronInterchangeDAO = new FiltronInterchangeDAO();
            IWixInterchangeManufacturerDAO wixInterchangeManufacturerDAO = new WixInterchangeManufacturerDAO();
            IFiltronInterchangeManufacturerDAO filtronInterchangeManufacturerDAO = new FiltronInterchangeManufacturerDAO();
            IWixFilterDAO wixFilterDAO = new WixFilterDAO();
            IFiltronFilterDAO filtronFilterDAO = new FiltronFilterDAO();
            List<Replacement> replacementList = new List<Replacement>();
            List<String> HelperList = new List<String>();

            IQueryable<FiltronFilter> filtronFiltersList = filtronFilterDAO.FindByWixFilterNumber(typedValue);
            if (filtronFiltersList != null) {
                foreach (FiltronFilter singleFiltronFilter in filtronFiltersList) {
                    String filterNumber = String.Empty;
                    if (String.IsNullOrEmpty(singleFiltronFilter.filter_wix_number))
                    {
                        filterNumber = singleFiltronFilter.filter_wix_number_hd;
                    }   //if hd only
                    else if (String.IsNullOrEmpty(singleFiltronFilter.filter_wix_number_hd))
                    {
                        filterNumber = singleFiltronFilter.filter_wix_number;
                    }   //else not hd only
                    else
                    {
                        filterNumber = singleFiltronFilter.filter_wix_number + "<br/>" + singleFiltronFilter.filter_wix_number_hd;
                    }   //else

                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append("<div class='col-xs-12 no-padding overlay-engine overlay-engine-boder-b'>");
                    sb.Append("<div class='col-xs-4 no-padding overlay-engine-boder-r'>");
                    sb.Append("<p>" + translationHelper.getTranslation("_repForeignFilter", languageName) + "</p>");
                    sb.Append("</div>");
                    sb.Append("<div class='col-xs-4 no-padding overlay-engine-boder-r'>");
                    sb.Append("<p >" + translationHelper.getTranslation("_repManufacturerName", languageName) + "</p>");
                    sb.Append("</div>");

                    sb.Append("<div class='col-xs-4 no-padding overlay-engine-boder-r'>");
                    sb.Append("<p >" + translationHelper.getTranslation("_repReplacementFilter", languageName) + "</p>");
                    sb.Append("</div>");
                    sb.Append("</div>");
                    sb.Append("<div id =\"filtersTable\">");

                    sb.Append("<div class=\"col-xs-12 no-padding overlay-engine engine-hover overlay-engine-boder-b filter-selector \"   data-model=\"" + singleFiltronFilter.filter_wix_number + "\"  data-filter=\"" + singleFiltronFilter.filter_id + "\" data-rep=\"F\"  >");
                        sb.Append("<div class=\"col-xs-4 no-padding overlay-engine-boder-r\">");
                        sb.Append("<p class=\"overlay-cross engine-info-normal\">" + singleFiltronFilter.filter_wix_number + "</p>");
                        sb.Append("</div>");
                        sb.Append("<div class=\"col-xs-4 no-padding overlay-engine-boder-r\">");
                        sb.Append("<p class=\"overlay-cross engine-info-normal\">" + "WIX" + "</p>");
                        sb.Append("</div>");
                        sb.Append("<div class=\"col-xs-4 no-padding overlay-engine-boder-r\">");
                        sb.Append("<p class=\"overlay-cross engine-info-normal\">" + filterNumber + "</p>");
                        sb.Append("</div>");
                        sb.Append("</div>");
  
                    sb.Append("</div>");
                    return sb.ToString();

                //    return "{\"bindings\": [{\"Part number\": \"" + Helpers.CatalogueHelpers.FiltronHelper.GetFiltronFilterWixNumber(singleFiltronFilter.filter_wix_number, singleFiltronFilter.filter_wix_number_hd) + "\", \"Manufacturer\": \"WIX\", \"repId\": \"" + singleFiltronFilter.filter_id + "\", \"WIX Filter\": \"" + filterNumber + "\", \"repFilterProda\": \"F\"}]}";
                }
            }

            IQueryable<WixFilter> wixFiltersList = wixFilterDAO.FindByFilterNumberEquals(typedValue);
            if (wixFiltersList != null)
            {
                foreach (WixFilter singleWixFilter in wixFiltersList)
                {
                    Replacement singleReplacement = new Replacement();


                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append("<div class='col-xs-12 no-padding overlay-engine overlay-engine-boder-b'>");
                    sb.Append("<div class='col-xs-4 no-padding overlay-engine-boder-r'>");
                    sb.Append("<p>" + translationHelper.getTranslation("_repForeignFilter", languageName) + "</p>");
                    sb.Append("</div>");
                    sb.Append("<div class='col-xs-4 no-padding overlay-engine-boder-r'>");
                    sb.Append("<p >" + translationHelper.getTranslation("_repManufacturerName", languageName) + "</p>");
                    sb.Append("</div>");

                    sb.Append("<div class='col-xs-4 no-padding overlay-engine-boder-r'>");
                    sb.Append("<p >" + translationHelper.getTranslation("_repReplacementFilter", languageName) + "</p>");
                    sb.Append("</div>");
                    sb.Append("</div>");
                    sb.Append("<div id =\"filtersTable\">");




                    sb.Append("<div class=\"col-xs-12 no-padding overlay-engine engine-hover overlay-engine-boder-b filter-selector \"   data-model=\"" + singleWixFilter.filter_number + "\"  data-filter=\"" + singleWixFilter.filter_id + "\" data-rep=\"W\"  >");
                    sb.Append("<div class=\"col-xs-4 no-padding overlay-engine-boder-r\">");
                    sb.Append("<p class=\"overlay-cross engine-info-normal\">" + singleWixFilter.filter_number + "</p>");
                    sb.Append("</div>");
                    sb.Append("<div class=\"col-xs-4 no-padding overlay-engine-boder-r\">");
                    sb.Append("<p class=\"overlay-cross engine-info-normal\">" + "WIX" + "</p>");
                    sb.Append("</div>");
                    sb.Append("<div class=\"col-xs-4 no-padding overlay-engine-boder-r\">");
                    sb.Append("<p class=\"overlay-cross engine-info-normal\">" + singleWixFilter.filter_number + "</p>");
                    sb.Append("</div>");
                    sb.Append("</div>");

                    sb.Append("</div>");
                    return sb.ToString();






                 //   return "{\"bindings\": [{\"Part number\": \"" + singleWixFilter.filter_number + "\", \"Manufacturer\": \"WIX\", \"repId\": \"" + singleWixFilter.filter_id + "\", \"WIX Filter\": \"" + singleWixFilter.filter_number + "\", \"repFilterProd\": \"W\"}]}";
                }
            }

            //equalsWix

            IQueryable<FiltronInterchange> filtronInterchangesCollectionEquals = filtronInterchangeDAO.FindByForeignFilterNameEquals(typedValue);
            if (filtronInterchangesCollectionEquals != null)
            {
                foreach (FiltronInterchange singleFiltronInterchange in filtronInterchangesCollectionEquals)
                {
                    String manufacturerName = filtronInterchangeManufacturerDAO.FindByPK(singleFiltronInterchange.manufacturer_id).manufacturer_name;
                    FiltronFilter filterEntity = filtronFilterDAO.FindByPK(singleFiltronInterchange.filter_id);
                    String filterNumber = String.Empty;
                    if (String.IsNullOrEmpty(filterEntity.filter_wix_number))
                    {
                        filterNumber = filterEntity.filter_wix_number_hd;
                    }   //if hd only
                    else if (String.IsNullOrEmpty(filterEntity.filter_wix_number_hd))
                    {
                        filterNumber = filterEntity.filter_wix_number;
                    }   //else not hd only
                    else
                    {
                        filterNumber = filterEntity.filter_wix_number + "<br/>" + filterEntity.filter_wix_number_hd;
                    }   //else
                    if (!String.IsNullOrEmpty(filterNumber))
                    {
                        Replacement singleReplacement = new Replacement();
                        singleReplacement.foreignFilter = singleFiltronInterchange.foreign_filter_number;
                        singleReplacement.foreignFilterManufacturer = manufacturerName;
                        singleReplacement.ownFilterProducer = "F";
                        singleReplacement.ownFilterId = singleFiltronInterchange.filter_id;
                        singleReplacement.ownFilterNumber = filterNumber;
                        string stringToCheck = removeSpecialCharacters(singleReplacement.foreignFilter + singleReplacement.ownFilterNumber + manufacturerName);
                        if (!(HelperList.Contains(stringToCheck)))
                        {
                            replacementList.Add(singleReplacement);
                            HelperList.Add(stringToCheck);
                        }
                    }   //if
                }
            }

            //if (replacementList.Any())
            //{
            //    return generateDataToTable(replacementList, languageName);
            //}

            IQueryable<WixInterchange> wixInterchangesCollectionEquals = wixInterchangeDAO.FindByForeignFilterNameEquals(typedValue);
            if (wixInterchangesCollectionEquals != null)
            {
                foreach (WixInterchange singleWixInterchange in wixInterchangesCollectionEquals)
                {
                    String manufacturerName = wixInterchangeManufacturerDAO.FindByPK(singleWixInterchange.manufacturer_id).manufacturer_name;
                    String filterNumber = wixFilterDAO.FindByPK(singleWixInterchange.filter_id).filter_number;
                    Replacement singleReplacement = new Replacement();
                    singleReplacement.foreignFilter = singleWixInterchange.foreign_filter_number;
                    singleReplacement.foreignFilterManufacturer = manufacturerName;
                    singleReplacement.ownFilterProducer = "W";
                    singleReplacement.ownFilterId = singleWixInterchange.filter_id;
                    singleReplacement.ownFilterNumber = filterNumber;
                    string stringToCheck = removeSpecialCharacters(singleReplacement.foreignFilter + singleReplacement.ownFilterNumber + manufacturerName);
                    if (!(HelperList.Contains(stringToCheck)))
                    {
                        replacementList.Add(singleReplacement);
                        HelperList.Add(stringToCheck);
                    }
                }
            }

            if (replacementList.Any())
            {
                return generateDataToTable(replacementList, languageName);
            }

            IQueryable<WixInterchange> wixInterchangesCollection = wixInterchangeDAO.FindByForeignFilterNameLimited(typedValue);
            if (wixInterchangesCollection != null) {
                foreach (WixInterchange singleWixInterchange in wixInterchangesCollection) {
                    String manufacturerName = wixInterchangeManufacturerDAO.FindByPK(singleWixInterchange.manufacturer_id).manufacturer_name;
                    String filterNumber = wixFilterDAO.FindByPK(singleWixInterchange.filter_id).filter_number;
                    Replacement singleReplacement = new Replacement();
                    singleReplacement.foreignFilter = singleWixInterchange.foreign_filter_number;
                    singleReplacement.foreignFilterManufacturer = manufacturerName;
                    singleReplacement.ownFilterProducer = "W";
                    singleReplacement.ownFilterId = singleWixInterchange.filter_id;
                    singleReplacement.ownFilterNumber = filterNumber;
                    string stringToCheck = removeSpecialCharacters(singleReplacement.foreignFilter + singleReplacement.ownFilterNumber + manufacturerName);
                    if (!(HelperList.Contains(stringToCheck)))
                    {
                        replacementList.Add(singleReplacement);
                        HelperList.Add(stringToCheck);
                    }
                }
            }

            IQueryable<FiltronInterchange> filtronInterchangesCollection = filtronInterchangeDAO.FindByForeignFilterNameLimited(typedValue);
            if (filtronInterchangesCollection != null) {
                foreach (FiltronInterchange singleFiltronInterchange in filtronInterchangesCollection)
                {
                    String manufacturerName = filtronInterchangeManufacturerDAO.FindByPK(singleFiltronInterchange.manufacturer_id).manufacturer_name;
                    FiltronFilter filterEntity = filtronFilterDAO.FindByPK(singleFiltronInterchange.filter_id);
                    String filterNumber = String.Empty;
                    if (String.IsNullOrEmpty(filterEntity.filter_wix_number))
                    {
                        filterNumber = filterEntity.filter_wix_number_hd;
                    }   //if hd only
                    else if (String.IsNullOrEmpty(filterEntity.filter_wix_number_hd))
                    {
                        filterNumber = filterEntity.filter_wix_number;
                    }   //else not hd only
                    else
                    {
                        filterNumber = filterEntity.filter_wix_number + "<br/>" + filterEntity.filter_wix_number_hd;
                    }   //else
                    if (!String.IsNullOrEmpty(filterNumber))
                    {                        
                        Replacement singleReplacement = new Replacement();
                        singleReplacement.foreignFilter = singleFiltronInterchange.foreign_filter_number;
                        singleReplacement.foreignFilterManufacturer = manufacturerName;
                        singleReplacement.ownFilterProducer = "F";
                        singleReplacement.ownFilterId = singleFiltronInterchange.filter_id;
                        singleReplacement.ownFilterNumber = filterNumber;
                        string stringToCheck = removeSpecialCharacters(singleReplacement.foreignFilter + singleReplacement.ownFilterNumber + manufacturerName);
                        if (!(HelperList.Contains(stringToCheck)))
                        {
                            replacementList.Add(singleReplacement);
                            HelperList.Add(stringToCheck);
                        }
                    }   //if
                }
            }

            if (replacementList.Any()) {
                return generateDataToTable(replacementList, languageName);
            }
            return translationHelper.getTranslation("_ajaxNoReplacements", languageName);
        }

        private static string generateDataToTable(List<Replacement> replacementList, string languageName)
        {           
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<div class='col-xs-12 no-padding overlay-engine overlay-engine-boder-b'>");
                     sb.Append("<div class='col-xs-4 no-padding overlay-engine-boder-r'>");
                          sb.Append("<p>" + translationHelper.getTranslation("_repForeignFilter", languageName) + "</p>");
                     sb.Append("</div>");    
                     sb.Append("<div class='col-xs-4 no-padding overlay-engine-boder-r'>");
                          sb.Append("<p >" + translationHelper.getTranslation("_repManufacturerName", languageName) + "</p>");
                     sb.Append("</div>");    
         
            sb.Append("<div class='col-xs-4 no-padding overlay-engine-boder-r'>");
            sb.Append("<p >"+translationHelper.getTranslation("_repReplacementFilter", languageName)+"</p>");
            sb.Append("</div>");
            sb.Append("</div>");
            sb.Append("<div id =\"filtersTable\">");
            foreach (var replacement in replacementList)
            {
                sb.Append("<div class=\"col-xs-12 no-padding overlay-engine engine-hover overlay-engine-boder-b filter-selector \"   data-model=\"" + replacement.foreignFilter + "\"  data-filter=\"" + replacement.ownFilterId + "\"   data-rep=\"" + replacement.ownFilterProducer + "\">");
                 sb.Append("<div class=\"col-xs-4 no-padding overlay-engine-boder-r\">");
                 sb.Append("<p class=\"overlay-cross engine-info-normal\">" + replacement.foreignFilter + "</p>");
                 sb.Append("</div>");
                 sb.Append("<div class=\"col-xs-4 no-padding overlay-engine-boder-r\">");
                 sb.Append("<p class=\"overlay-cross engine-info-normal\">" + replacement.foreignFilterManufacturer + "</p>");
                 sb.Append("</div>");
                 sb.Append("<div class=\"col-xs-4 no-padding overlay-engine-boder-r\">");
                 sb.Append("<p class=\"overlay-cross engine-info-normal\">" + replacement.ownFilterNumber + "</p>");
                 sb.Append("</div>");
                 sb.Append("</div>");               
            }
            sb.Append("</div>"); 
            return sb.ToString();
        }

        private static string removeSpecialCharacters(string value)
        {
            if (String.IsNullOrEmpty(value))
            {
                return String.Empty;
            }   //if
            return Regex.Replace(value, @"[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled);
        }

        public static List<Replacement> generateReplacementListFiltronOe(int filtronFilterId)
        {
            IFiltronInterchangeDAO filtronInterchangeDAO = new FiltronInterchangeDAO();
            IFiltronInterchangeManufacturerDAO filtronInterchangeManufacturerDAO = new FiltronInterchangeManufacturerDAO();
            IFiltronFilterDAO filtronFilterDAO = new FiltronFilterDAO();
            List<Replacement> replacementList = new List<Replacement>();
            List<String> HelperList = new List<String>();
            //equals
            IQueryable<FiltronInterchange> filtronInterchangesCollectionEquals = filtronInterchangeDAO.FindByOwnFilterNameEqualsOnlyOe(filtronFilterId);
            if (filtronInterchangesCollectionEquals != null)
            {
                foreach (FiltronInterchange singleFiltronInterchange in filtronInterchangesCollectionEquals)
                {
                    String manufacturerName = filtronInterchangeManufacturerDAO.FindByPK(singleFiltronInterchange.manufacturer_id).manufacturer_name;
                    FiltronFilter filterEntity = filtronFilterDAO.FindByPK(singleFiltronInterchange.filter_id);
                    String filterNumber = String.Empty;
                    if (String.IsNullOrEmpty(filterEntity.filter_wix_number))
                    {
                        filterNumber = filterEntity.filter_wix_number_hd;
                    }   //if hd only
                    else if (String.IsNullOrEmpty(filterEntity.filter_wix_number_hd))
                    {
                        filterNumber = filterEntity.filter_wix_number;
                    }   //else not hd only
                    else
                    {
                        filterNumber = filterEntity.filter_wix_number + "<br/>" + filterEntity.filter_wix_number_hd;
                    }   //else
                    if (!String.IsNullOrEmpty(filterNumber))
                    {
                        Replacement singleReplacement = new Replacement();
                        singleReplacement.foreignFilter = singleFiltronInterchange.foreign_filter_number;
                        singleReplacement.foreignFilterManufacturer = manufacturerName;
                        singleReplacement.ownFilterProducer = "F";
                        singleReplacement.ownFilterId = singleFiltronInterchange.filter_id;
                        singleReplacement.ownFilterNumber = filterNumber;
                        string stringToCheck = removeSpecialCharacters(singleReplacement.foreignFilter + singleReplacement.ownFilterNumber + manufacturerName);
                        if (!(HelperList.Contains(stringToCheck)))
                        {
                            replacementList.Add(singleReplacement);
                            HelperList.Add(stringToCheck);
                        }
                    }   //if
                }
            }
            if (replacementList != null)
            {
                replacementList = replacementList.OrderBy(r => r.foreignFilterManufacturer).ToList();
            }   //if
            return replacementList;
        }
    }
}
